import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { ElMessage } from 'element-plus'
// 导入个人的组件
import AuthorityManagement from './AuthorityManagement'
import ClassManagement from './ClassManagement'
import Home from './Home'
import Organization from './Organization'
import UserManagement from './UserManagement'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/Home/PersonalHomePage'
  },
  {
    path: '/Home',
    name: 'Home',
    component: () => import('@/pages/Home/Home.vue'),
    children: [
      ...Home,
      ...AuthorityManagement,
      ...ClassManagement,
      ...Organization,
      ...UserManagement
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "introduce" */ '../pages/Login/login.vue')
  },
]
const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes
})
// 路由导航守卫
const notoken = ['/login', '/forgetPassword', '/register', '/index', '/list'] // 不需要tonken的页面
router.beforeEach((to: any, from: any, next: any) => {
  // NProgress.start();
  if (notoken.indexOf(to.path) == -1) {
    const token = sessionStorage.getItem('token')
    if (token == null || token == '') {
      ElMessage.info('您不在登录状态，请先登录!')
      router.replace('/login')
    }
  }
  next()
})

export default router